<?php
namespace app\admin\controller;
use think\Controller;
use Common\Controller\BaseController;
use think\Request;
use think\Session;
class Index extends Controller
{
//后台首页开始
    public function index()
    {
        $user = session('user');
        //dump($user);exit;
        if($user){
            $name['name'] = session('user.name');
            //dump($name);exit;
            $res = db('admin')->where($name)->find();
            $this->assign('list',$res);
            return $this->fetch();
        }else{
            header("Refresh:1;url=../login/login");
        }
    }
//后台首页结束
//exel 导出开始
//中文导出开始
public function chinese(){
    $user = session('user');
    //dump($user);exit;
    if(!$user){
        header("Refresh:1;url=login");
    }
    //echo "<script>alert('中文导出开发中')</script>";
    //数据库字段
    //$list = array(array('id'=>1,'name'=>'du'),array('id'=>2,'name'=>'long'));
    $list = db('user')->select();
    //导出
    $path = dirname(__FILE__); //找到当前脚本所在路径
    vendor("PHPExcel.Classes.PHPExcel");
    vendor("PHPExcel.Classes.PHPExcel.Writer.IWriter");
    vendor("PHPExcel.Classes.PHPExcel.Writer.Abstract");
    vendor("PHPExcel.Classes.PHPExcel.Writer.Excel5");
    vendor("PHPExcel.Classes.PHPExcel.Writer.Excel2007");
    vendor("PHPExcel.Classes.PHPExcel.IOFactory");
    $objPHPExcel = new \PHPExcel();
    $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
    $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    // 实例化完了之后就先把数据库里面的数据查出来
    // 设置表头信息
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'id')
        ->setCellValue('B1', '发货姓名')
        ->setCellValue('C1', '发货手机号')
        ->setCellValue('D1', '发货城市')
        ->setCellValue('E1', '快递单号')
        ->setCellValue('F1', '发货地址')
        ->setCellValue('G1', '发件单位名称')
        ->setCellValue('H1', '运费金额')
        ->setCellValue('I1', '重量')
        ->setCellValue('J1', '数量')
        ->setCellValue('K1', '保险价格')
        ->setCellValue('L1', '揽收员工')
        ->setCellValue('M1', '总计费用')
        ->setCellValue('N1', '快递物品')
        ->setCellValue('O1', '收件人姓名')
        ->setCellValue('P1', '收件人手机号')
        ->setCellValue('Q1', '收件人地址')
        ->setCellValue('R1', '收件人单位名称')
        ->setCellValue('S1', '收件城市')
        ->setCellValue('T1', '签收人员');
    /*--------------开始从数据库提取信息插入Excel表中------------------*/
    $i=2;  //定义一个i变量，目的是在循环输出数据是控制行数
    $count = count($list);  //计算有多少条数据
    for ($i = 2; $i <= $count+1; $i++) {
        $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $list[$i-2]['id']);
        $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $list[$i-2]['hairname']);
        $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $list[$i-2]['hairphone']);
        $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $list[$i-2]['haircity']);
        $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $list[$i-2]['oddnumbers']);
        $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $list[$i-2]['hairaddress']);
        $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $list[$i-2]['haircompany']);
        $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $list[$i-2]['pay']);
        $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $list[$i-2]['weight']);
        $objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $list[$i-2]['number']);
        $objPHPExcel->getActiveSheet()->setCellValue('K' . $i, $list[$i-2]['insuredprice']);
        $objPHPExcel->getActiveSheet()->setCellValue('L' . $i, $list[$i-2]['collect']);
        $objPHPExcel->getActiveSheet()->setCellValue('M' . $i, $list[$i-2]['totalpay']);
        $objPHPExcel->getActiveSheet()->setCellValue('N' . $i, $list[$i-2]['article']);
        $objPHPExcel->getActiveSheet()->setCellValue('O' . $i, $list[$i-2]['collectname']);
        $objPHPExcel->getActiveSheet()->setCellValue('P' . $i, $list[$i-2]['collectphone']);
        $objPHPExcel->getActiveSheet()->setCellValue('Q' . $i, $list[$i-2]['collectaddress']);
        $objPHPExcel->getActiveSheet()->setCellValue('R' . $i, $list[$i-2]['collectompany']);
        $objPHPExcel->getActiveSheet()->setCellValue('S' . $i, $list[$i-2]['collectcity']);
        $objPHPExcel->getActiveSheet()->setCellValue('T' . $i, $list[$i-2]['signstaff']);
    }
    /*--------------下面是设置其他信息------------------*/

    $objPHPExcel->getActiveSheet()->setTitle('productaccess');      //设置sheet的名称
    $objPHPExcel->setActiveSheetIndex(0);                   //设置sheet的起始位置
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');   //通过PHPExcel_IOFactory的写函数将上面数据写出来

    $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");

    header('Content-Disposition: attachment;filename="快递信息.xlsx"');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

    $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
}
//中文导出结束

}
